package com.itdianzi.controller.system;

import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.system.Module;
import com.itdianzi.domain.system.Role;
import com.itdianzi.service.system.ModuleService;
import com.itdianzi.service.system.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    public RoleService roleService;
    @RequestMapping(value = "/list",name = "角色查询")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        PageInfo pageInfo = roleService.findAll(page, size, companyId);
        request.setAttribute("page",pageInfo);
        return "/system/role/role-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转到添加角色页面")
    public String toAdd() {

        return "/system/role/role-add";
    }
    @RequestMapping(value = "/edit",name = "添加或修改角色信息")
    public String edit(Role role) {
        role.setCompanyId(companyId);
        role.setCompanyName(companyName);

        if (StringUtils.isBlank(role.getId())) {
            roleService.save(role);
        } else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";

    }
    @RequestMapping(value = "/toUpdate",name = "跳转到角色修改页面")
    public String toUpdate(String id) {
        Role role = roleService.findById(id);
        request.setAttribute("role",role);


        return "/system/role/role-update";
    }
    @RequestMapping(value = "/delete",name = "角色数据删除")
    public String delete(String id) {
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule",name = "跳转分配角色权限的页面")
    public String roleModule(String roleid) {
        //查询角色信息
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "/system/role/role-module";
    }
    @Autowired
    private ModuleService moduleService;

    /**
     * 返回ajax的数据
     * [{} , {} , {} ]
     * [
     *  {id:11, pId:1, name:"随意勾选 1-1", checked :true,open:true},
     *  { id:111, pId:11, name:"随意勾选 1-1-1"},
     *  { id:112, pId:11, name:"随意勾选 1-1-2"}
     *  ]
     *  id == 模块id
     *  name == 模块名称
     *  pid == 模块中的父模块id
     *  [] 表示数组 或者 list集合
     *  {} 表示对象或者Map  此处使用Map 如果是对象 还需要构建一个对象 而map直接实例化即可
     * @return
     */
    @RequestMapping(value = "/initTree",name = "角色返回ajax页面数据")
    @ResponseBody//响应json的数据
    public List<Map> initTree(String roleid){
        //创建一个要返回的json数据List集合
        List<Map> mapList = new ArrayList<>();
        //查询所有的模块数据 转换成 List<Map>
        List<Module> moduleList = moduleService.findAll();//查询出10个

        //查询当前角色的模块信息数据 根据角色id  最后返回的 List<Module>
        //System.err.println(roleid);
        //当前角色模块数据  一定少于等于所有 moduleList数据
        List<Module> roleModules =moduleService.findByRole(roleid); //查询出5个

        //转换
        for (Module module : moduleList) {
            //定义每一条数据
            Map<String , Object > map = new HashMap<>();
            map.put("id" , module.getId());
            map.put("name" , module.getName());
            map.put("pId" ,module.getParentId() );
            //map.put("checked" , true);

            //如果使用包含必须重写equals方法
            boolean flag = roleModules.contains(module);
            if(flag){
                map.put("checked" ,true);
            }
            /*for (Module roleModule : roleModules) {
                //判断包含关系
                if(module.getId().equals(roleModule.getId())){
                    map.put("checked" ,true);
                }
            }*/
            //判断是不是一级菜单?
            //建立关系
            mapList.add(map);
        }

        return mapList;
    }

    /**
     * 修改权限 : 修改中间表的数据
     * roleid: 4028a1c34ec2e5c8014ec2ebf8430001
     * moduleIds: 2,201,202,205,206,207,208,3,301,302,303
     *
     * @return
     */
    @RequestMapping(value = "/updateRoleModule",name = "修改角色和模块信息")
    public String updateRoleModule(String roleid, String moduleIds) {
        moduleService.updateRoleModule(roleid, moduleIds);
        return "redirect:/system/role/list.do";
    }
}
